/*
 * ====================================================================
 * Copyright (C) 2013 Eric Blond
 *
 * This file is part of Neptune.
 *
 * Neptune is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Neptune is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Neptune.  If not, see <http://www.gnu.org/licenses/>.
 * ====================================================================
 */

package com.hyperplan.neptune.utils;

public class Location implements Comparable<Location> {

	private int beginLine;
	private int beginColumn;
	private int endLine;
	private int endColumn;
	
	public Location(
			int beginLine,
			int beginColumn,
			int endLine,
			int endColumn) {
		
		this.beginLine = beginLine;
		this.beginColumn = beginColumn;
		this.endLine = endLine;
		this.endColumn = endColumn;
	}
	
	public Location(int beginLine, int beginColumn, int length) {
		this(beginLine, beginColumn, beginLine, beginColumn + length - 1);
	}
	
	public int getBeginLine() {
		return beginLine;
	}
	
	public int getBeginColumn() {
		return beginColumn;
	}
	
	public int getEndLine() {
		return endLine;
	}
	
	public int getEndColumn() {
		return endColumn;
	}
	
	public String description() {
		if (beginLine == endLine) {
			if (beginColumn == endColumn) {
				return String.format("(%s, %s)", beginLine, beginColumn);				
			} else {
				return String.format("(%s, %s-%s)",
						beginLine, beginColumn, endColumn);
			}
		} else {
			return String.format("(%s, %s)-(%s, %s)",
					beginLine, beginColumn, endLine, endColumn);
		}
	}
	
	public Location join(Location location) {
		return new Location(beginLine, beginColumn,
				location.getEndLine(), location.getEndColumn());
	}

	@Override
	public int compareTo(Location location) {
		if (beginLine < location.getBeginLine()) {
			return -1;
		} else if (beginLine > location.getBeginLine()) {
			return 1;
		} else {
			if (beginColumn < location.getBeginColumn()) {
				return -1;
			} else if (beginColumn > location.getBeginColumn()) {
				return 1;
			} else {
				return 0;
			}
		}
	}
}
